/*
 * 文件名：ParkwellMemberController.java	 
 * 时     间：上午11:21:53
 * 作     者：Administrator       
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.local.controller.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.threads.UploadMemberInfoRunble;
import com.cz073586.tools.util.ThreadPoolUtil;
import com.local.dao.mapers.LocalMemberMapper;
import com.local.manager.cfgManager.CloudConfigManager;
import com.local.model.FreeCar;
import com.local.model.Integration;
import com.local.model.LocalCarType;
import com.local.model.LocalCard;
import com.local.model.LocalChargeBytimeType;
import com.local.model.LocalMember;
import com.local.page.FreeCarPage;
import com.local.page.IntegrationPage;
import com.local.page.LocalMemberPage;
import com.local.service.face.FreeCarService;
import com.local.service.face.IntegrationService;
import com.local.service.face.LocalChargeByTimeTypeService;
import com.local.service.face.LogService;
import com.local.service.face.ParkwellMemberService;
import com.qianhai.common.constants.CommonContans;
import com.qianhai.common.controller.BaseController;
import com.qianhai.common.entity.LoginUserInfo;
import com.qianhai.common.utils.Tools;

/**
 * 类描述:停车场会员和临时会员
 * 
 * @ClassName: ParkwellMemberController
 * @author Administrator
 * @date 2015年4月7日 上午11:21:53
 */
@RestController
@RequestMapping(value = "/admin/member", method = RequestMethod.POST)
public class ParkwellMemberController extends BaseController
{
	@Autowired
	private ParkwellMemberService parkwellMemberService;
	
	@Autowired
	private LocalChargeByTimeTypeService localChargeByTimeTypeService;
	
	@Autowired
	private LogService logService;
	
	@Autowired
	private LocalMemberMapper localMemberMapper;
	
	@Autowired
	private IntegrationService integrationService;
	
	@Autowired
	private FreeCarService freeCarService;
	
	@RequestMapping(value = "/loadCard.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap loadCard()
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			List<LocalCard> li = parkwellMemberService.queryAllCard();
			resultMap.put(CommonContans.STRUTS_DATA, li);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/loadCartype.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap loadCartype()
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			List<LocalCarType> li = parkwellMemberService.queryAllCar();
			resultMap.put(CommonContans.STRUTS_DATA, li);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/loadCartypeCharge.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap loadCartypeCharge()
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			List<LocalChargeBytimeType> li = localChargeByTimeTypeService
					.queryAllCar();
			resultMap.put(CommonContans.STRUTS_DATA, li);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：添加或者修改一条会员记录
	 * 
	 * @Title: addOrModifyMember
	 * @param @param member
	 * @param @return
	 * @return ModelMap
	 * @user Administrator 2015年4月7日
	 * @throws
	 */
	@RequestMapping(value = "/addOrModifyMember.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap addOrModifyMember(@RequestBody LocalMember member)
	{
		ModelMap resultMap = new ModelMap();
		LoginUserInfo userInfo=	this.getLoginUser();
		if(!Tools.isEmpty(userInfo)){
			member.setOperateId(userInfo.getUuid());
			member.setOperateName(userInfo.getLoginName());
		}

	
		try
		{
			//根据当前操作用户，获取当前停车场。
			String memberUuid=parkwellMemberService.saveOrUpdateParkwellMember(member);
			if(!Tools.isEmpty(memberUuid)){
				//会员车主信息，上传到云平台
				if (CloudConfigManager.instance().isOpen())
				{
					member.setParkCode(CloudConfigManager.instance().getExchangeId());
					UploadMemberInfoRunble upload = new UploadMemberInfoRunble(
							member, logService);
					ThreadPoolUtil.instance().execute(upload);
				}
			}
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：查询会员列表并分页
	 * 
	 * @Title: queryMemberListByPage
	 * @param @param pageInfo
	 * @param @return
	 * @return ModelMap
	 * @user Administrator 2015年4月7日
	 * @throws
	 */
	@RequestMapping(value = "/queryMemberByPage.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap queryMemberListByPage(
			@RequestBody LocalMemberPage<LocalMember> pageInfo)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			parkwellMemberService.findAllParkwellMemberByQueryForm(pageInfo);
			resultMap.put(CommonContans.STRUTS_DATA, pageInfo);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：查询会员信息
	 * 
	 * @Title: queryMemberById
	 * @param @param memberUuid
	 * @param @return
	 * @return ModelMap
	 * @user Administrator 2015年4月7日
	 * @throws
	 */
	@RequestMapping(value = "/queryMemberById.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap queryMemberById(String memberUuid)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			
			resultMap.put(CommonContans.STRUTS_DATA,
					parkwellMemberService.selectByPrimaryKey(memberUuid));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：通过车牌号码，查找会员信息
	 * 
	 * @Title: queryMemBerByCarNo
	 * @param @param carNo
	 * @param @return
	 * @return ModelMap
	 * @user Administrator 2015年6月9日
	 * @throws
	 */
	
	@RequestMapping(value = "/queryMemBerByCarNo.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap queryMemBerByCarNo(@RequestBody LocalMember member)
	{
		ModelMap resultMap = new ModelMap();
		List<LocalMember> pm = parkwellMemberService
				.findParkwellMemberByCarNo(member.getCarNo());
		try
		{
			resultMap.put(CommonContans.STRUTS_DATA, pm.get(0));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：删除会员信息，以及关联的信息。
	 * 
	 * @Title: deleteMember
	 * @param @param memberCarUuid
	 * @param @param parkUuid
	 * @param @return
	 * @return ModelMap
	 * @user Administrator 2015年6月15日
	 * @throws
	 */
	@RequestMapping(value = "/deleteMember.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap deleteMember(String memberUuid)
	{
		ModelMap resultMap = new ModelMap();
		boolean flag = false;
		try
		{
			LocalMember member = localMemberMapper
					.selectByPrimaryKey(memberUuid);
			member.setOperate(5);
			flag = parkwellMemberService.deleteByPrimaryKey(memberUuid);
			// 会员车主信息，上传到云平台
			if (CloudConfigManager.instance().isOpen())
			{
				member.setParkCode(CloudConfigManager.instance()
						.getExchangeId());
				UploadMemberInfoRunble upload = new UploadMemberInfoRunble(
						member, logService);
				ThreadPoolUtil.instance().execute(upload);
			}
			resultMap.put(CommonContans.STRUTS_DATA, flag);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：查询会员列表并分页
	 * 
	 * @Title: queryMemberFreeListByPage
	 * @param @param pageInfo
	 * @param @return
	 * @return ModelMap
	 * @user Administrator 2015年4月7日
	 * @throws
	 */
	@RequestMapping(value = "/queryMemberFreeListByPage.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap queryMemberFreeListByPage(
			@RequestBody IntegrationPage<Integration> pageInfo)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			integrationService.queryMemberFreeQueryForm(pageInfo);
			resultMap.put(CommonContans.STRUTS_DATA, pageInfo);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：查询免费卡列表
	 * 
	 * @Title: queryFreeCarListByPage
	 * @param @param pageInfo
	 * @param @return
	 * @return ModelMap
	 * @user Administrator 2016年2月21日
	 * @throws
	 */
	@RequestMapping(value = "/queryFreeCarListByPage.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap queryFreeCarListByPage(
			@RequestBody FreeCarPage<FreeCar> pageInfo)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			freeCarService.queryFreeCarQueryForm(pageInfo);
			resultMap.put(CommonContans.STRUTS_DATA, pageInfo);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：添加或者修改一条会员记录
	 * 
	 * @Title: addOrModifyMember
	 * @param @param member
	 * @param @return
	 * @return ModelMap
	 * @user Administrator 2015年4月7日
	 * @throws
	 */
	@RequestMapping(value = "/addOrModifyMemberFree.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap addOrModifyMemberFree(@RequestBody Integration member)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			Integer memberUuid = integrationService.addOrUpdateByCarNo(member);
			
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/addOrModifyFreeCar.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap addOrModifyFreeCar(@RequestBody FreeCar freeCar)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			freeCarService.addFreeCar(freeCar);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：删除免费卡信息。
	 * 
	 * @Title: deleteMemberFree
	 * @param @param
	 * @param @param Uuid
	 * @param @return
	 * @return ModelMap
	 * @user Administrator 2015年6月15日
	 * @throws
	 */
	@RequestMapping(value = "/deleteMemberFree.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap deleteMemberFree(String uuid)
	{
		ModelMap resultMap = new ModelMap();
		try
		{
			integrationService.deleteByPrimaryKey(uuid);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/deleteFreeCar.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap deleteFreeCar(String uuid)
	{
		ModelMap resultMap = new ModelMap();
		try
		{
			freeCarService.deleteByPrimaryKey(uuid);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：查询会员信息
	 * 
	 * @Title: queryMemberFreeById
	 * @param @param uuid
	 * @param @return
	 * @return ModelMap
	 * @user Administrator 2015年4月7日
	 * @throws
	 */
	@RequestMapping(value = "/queryMemberFreeById.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap queryMemberFreeById(String uuid)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			
			resultMap.put(CommonContans.STRUTS_DATA,
					integrationService.selectByPrimaryKey(uuid));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/queryFreeCarById.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap queryFreeCarById(String uuid)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			
			resultMap.put(CommonContans.STRUTS_DATA,
					freeCarService.selectByPrimaryKey(uuid));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
}
